//
//  PlaceViewController.m
//  SimpleHappy
//
//  Created by lanou on 15/11/24.
//  Copyright (c) 2015年 RockyFung. All rights reserved.
//

#import "PlaceViewController.h"
#import "titleView.h"
#import "CollectionViewCell.h"
#import "ReserveController.h"
#import "Define.h"
#import "SHTabBarController.h"
@interface PlaceViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UICollectionViewDelegate>
@property(nonatomic,retain)NSMutableArray *array;
@property(nonatomic,retain)UICollectionView *collectionView;
@end

@implementation PlaceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self getInformation];
    CGFloat height = 64*KScreenWidth/375;
    titleView *title = [[titleView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, height)];
    title.titleLabel.text = @"选择城市";
    title.titleLabel.font = [UIFont systemFontOfSize:KScreenWidth / 18];
    [title.backButton addTarget:self action:@selector(backButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:title];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, height, KScreenWidth, KScreenHeight-height) collectionViewLayout:layout];
    //注册cell
    [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    layout.minimumInteritemSpacing=30*KScreenWidth/375;
    layout.minimumLineSpacing=30*KScreenWidth/375;
    layout.itemSize = CGSizeMake((KScreenWidth-120*KScreenWidth/375)/3, 50*KScreenWidth/375);
    [self.view addSubview:self.collectionView];
    UIView *myView=[[UIView alloc]initWithFrame:CGRectMake(0, height*1.2, KScreenWidth, 1)];
    myView.backgroundColor = [UIColor blackColor];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:myView];
}
#pragma mark----------请求数据---------------------
-(void)getInformation
{
    NSString *kurl = @"http://api.yhouse.com/m/city/dynmiclist";
    NSURL *url = [NSURL URLWithString:kurl];
    [[[NSURLSession sharedSession]dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        
        if (error) {
            NSLog(@"网络请求错误");
            return ;
        }
        
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:0];
        self.array = [[NSMutableArray alloc]init];
        NSDictionary *dataDic = dic[@"data"];
        for (NSDictionary *allCityDic in dataDic[@"allCity"])
        {
            CityModel *model = [[CityModel alloc]init];
            [model setValuesForKeysWithDictionary:allCityDic];
            [self.array addObject:model];
        }
        dispatch_sync(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];

        });
    }]resume];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.array.count;
    
}
//返回collectionView的cell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    CityModel *model = self.array[indexPath.row];
    cell.label.text = model.name;
    cell.label.textAlignment = NSTextAlignmentCenter;
    cell.label.font = [UIFont boldSystemFontOfSize:18*KScreenWidth/375];
    return cell;
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
   return UIEdgeInsetsMake(50*KScreenWidth/375, 30*KScreenWidth/375, 0, 30*KScreenWidth/375);
}
#pragma mark--------返回按钮的触发方法------------------
-(void)backButton
{
    SHTabBarController *shVc = [[SHTabBarController alloc]init];
    shVc.view.backgroundColor = [UIColor whiteColor];
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark------------cell被选中的时候调用的方法-------------
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CityModel *cityModel = self.array[indexPath.row];
    
    //block传值

    

    if (!cityModel.name)
    {
         self.block(cityModel);
    }
     

    [[NSUserDefaults standardUserDefaults] setObject:cityModel.name forKey:@"name"];
    [[NSUserDefaults standardUserDefaults] setInteger:cityModel.baseCityId forKey:@"id"];
    [self dismissViewControllerAnimated:YES completion:nil];




}
@end
